package com.woniuxy.owner.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.common.dto.FaceInfoDto;
import com.woniuxy.common.dto.OwnerDto;
import com.woniuxy.common.utils.FaceUploadResult;
import com.woniuxy.owner.feign.faceInfoFeign;
import com.woniuxy.owner.pojo.Owner;
import com.woniuxy.owner.service.OwnerService;
import com.woniuxy.owner.mapper.OwnerMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.annotation.security.DenyAll;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.woniuxy.common.constant.FinanceConstant.User.WONIU;
import static com.woniuxy.common.constant.FinanceConstant.User.WONIUXY;
import static com.woniuxy.common.utils.UserUtil.getUser1;

/**
* @author 86159
* @description 针对表【owner(业主信息表)】的数据库操作Service实现
* @createDate 2025-07-16 20:07:18
*/
@Service
@AllArgsConstructor
public class OwnerServiceImpl extends ServiceImpl<OwnerMapper, Owner>
        implements OwnerService {
    private faceInfoFeign faceInfoFeign;

    @Override
    public void addOwner(Owner owner) {
        Owner owner1 = new Owner();
        addOwner(owner, owner1);
        save(owner1);
        Owner one = lambdaQuery()
                .eq(Owner::getRealName, owner1.getRealName())
                .one();
        FaceUploadResult faceUploadResult = faceInfoFeign.uploadFace(one.getFaceImgSrc(), one.getId().toString());
        FaceInfoDto faceInfoDto = null;
        if (faceUploadResult.getFaceToken() != null) {
            faceInfoDto = new FaceInfoDto();
            faceInfoDto.setId(one.getId());
            faceInfoDto.setImagePath(faceUploadResult.getImagePath());
            faceInfoDto.setOwnerId(one.getId());
            faceInfoDto.setFeatureData(faceUploadResult.getFaceToken());
        }
        faceInfoFeign.addFaceInfo(faceInfoDto);
    }

    private static void addOwner(Owner owner, Owner owner1) {
        owner1.setRealName(owner.getRealName());
        owner1.setPhone(owner.getPhone());
        owner1.setUsername(owner.getUsername());
        owner1.setPassword(owner.getPassword());
        owner1.setFaceImgSrc(owner.getFaceImgSrc());
        owner1.setBuildingId(owner.getBuildingId());
        owner1.setCommunityId(owner.getCommunityId());
    }

    @Override
    public String login(String username, String password) {
        password = SecureUtil.md5(SecureUtil.md5(password) + WONIU);

        Owner user = lambdaQuery().eq(Owner::getRealName, username)
                .eq(Owner::getPassword, password)
                .one();
        //判断用户是否存在，不存在则返回null
        if (Objects.isNull(user)) {
            return null;
        }
        //如果存在则返回JWT
        Map<String, Object> payload = new HashMap<>(1);//已知长度的集合，创建集合时直接指定长度
        //脱敏 1、把敏感信息删除 2、返回没有敏感信息的对象 3、正则表达式 注：后台脱敏
        user.setPassword(null);
        user.setPhone(null);
        payload.put("user", user);
        return JWTUtil.createToken(payload, WONIUXY.getBytes());
    }

    @Override
    public Owner house() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 从Authorization头中获取Token
        String token = request.getHeader("Authorization");
        OwnerDto user = getUser1(token);
        return getById(user.getId());
    }
}




